/**
 *  
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 */
package com.aboverock.common.utils;

import javax.servlet.http.HttpServletRequest;

/**
 * 客户端IP地址获取工具类.
 *
 * @author Rock Wang
 */
public class ClientIpUtils {

    private static final String UNKNOWN = "unknown";

    /**
     * Instantiates a new client ip utils.
     */
    private ClientIpUtils() {
    }

    /**
     * Gets the ip addr.
     *
     * @param request the request
     * @return the ip addr
     */
    public static String getIpAddr(HttpServletRequest request) {
        if (request == null) {
            return UNKNOWN;
        }
        String ip = request.getHeader("x-forwarded-for");
        if (ip == null || ip.length() == 0 || UNKNOWN.equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || UNKNOWN.equalsIgnoreCase(ip)) {
            ip = request.getHeader("X-Forwarded-For");
        }
        if (ip == null || ip.length() == 0 || UNKNOWN.equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || UNKNOWN.equalsIgnoreCase(ip)) {
            ip = request.getHeader("X-Real-IP");
        }

        if (ip == null || ip.length() == 0 || UNKNOWN.equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
        }
        return ip;
    }
}